package com.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.demo.SessionStore;

/**
 * Servlet Filter implementation class PathFilter
 */
@WebFilter("/PathFilter")
public class PathFilter extends HttpFilter implements Filter {

	@Override
	protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		String currentURI = request.getRequestURI();
		String lastURI = currentURI.substring(currentURI.indexOf("/",1),currentURI.length());
		HttpSession session = request.getSession();
		if("/main.jsp".equals(lastURI) || "/manage.jsp".equals(lastURI)) {
				if(session.getAttribute(SessionStore.SESSION_USER)==null) {
					response.sendRedirect("login.jsp");
				}
			}
		chain.doFilter(request, response);
		}

	}
	

